package com.yixin.hospital.controller;

import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.dto.req.drugdepository.DrugDepositoryAddReqDTO;
import com.yixin.hospital.entity.dto.req.drugdepository.DrugDepositoryEditReqDTO;
import com.yixin.hospital.entity.vo.DeptListDrugDepVO;
import com.yixin.hospital.entity.vo.DrugDepositoryVo;
import com.yixin.hospital.service.DrugDepositoryService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/drug-depository")
@Api(value = "库房管理", tags = "库房管理")
public class DrugDepositoryController {

    @Resource
    private DrugDepositoryService drugDepositoryService;

    @GetMapping("/list")
    @ApiOperation(value = "库房列表")
    public ApiResult<List<DrugDepositoryVo>> list() {
        return drugDepositoryService.allDropList();
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加库房")
    public ApiResult<?> add(@Valid @RequestBody DrugDepositoryAddReqDTO drugDepositoryAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return drugDepositoryService.add(drugDepositoryAddReqDTO, userName);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑库房")
    public ApiResult<?> edit(@Valid @RequestBody DrugDepositoryEditReqDTO drugDepositoryEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return drugDepositoryService.edit(drugDepositoryEditReqDTO, userName);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除库房")
    public ApiResult<?> remove(@PathVariable int id) {
        drugDepositoryService.removeById(id);
        return ApiResult.success();
    }

    @GetMapping("/dept-list/{deptId}")
    @ApiOperation(value = "部门及其下属部门库房列表")
    public ApiResult<List<DeptListDrugDepVO>> deptList(@PathVariable Long deptId) {
        return drugDepositoryService.deptListByDeptId(deptId);
    }

    @GetMapping("/available-list-optType")
    @ApiOperation(value = "可用库房列表")
    public ApiResult<List<DrugDepositoryVo>> availableList(HttpServletRequest request, @RequestParam Integer optType) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return drugDepositoryService.getAvailableListByOptType(optType, userId);
    }

}
